package com.yiwei.jmx.activity;

import android.annotation.TargetApi;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.BankAuthBean;
import com.yiwei.jmx.api.bean.SmsSendBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.constant.UserInfoGlobal;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.SelectAreaDilaog;
import com.yiwei.jmx.view.SelectMenuTipsDilaog;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class BankBindActivity extends BaseActivity {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.img_icon_center)
    ImageView imgIconCenter;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_idno)
    EditText editIdno;
    @BindView(R.id.edit_bank_no)
    EditText editBankNo;
    @BindView(R.id.edit_bank_type)
    EditText editBankType;
    @BindView(R.id.edit_address)
    TextView editAddress;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.cb_check)
    ImageView cbCheck;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.txt_bank_tips)
    TextView txtBankTips;
    private SelectMenuTipsDilaog selectMenuTipsDilaog;
    private SelectAreaDilaog selectAreaDilaog;
    private Unbinder unbiner;
    private static final int TIME = 60 * 1000;  //验证码倒计时时间
    private CountDownTimer timer = new CountDownTimer(TIME, 1000) {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onTick(long millisUntilFinished) {
            try {
                btnSendCode.setEnabled(false);
                btnSendCode.setText((millisUntilFinished / 1000) + "s重新获取");
//                btnSendCode.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray));
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFinish() {
            resetBtn();
        }
    };


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void resetBtn() {
        if (timer != null) timer.cancel();
        btnSendCode.setEnabled(true);
        btnSendCode.setText("重新发送");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_bank);
        initBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    @OnTextChanged({R.id.edit_phone, R.id.edit_code})
    void onTextChange(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPhone)) && !TextUtils.isEmpty(UI.toString(editCode))&& !TextUtils.isEmpty(UI.toString(editBankNo))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    private void getBankAuth(String mobile, String bankcard, String verify_code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        BankAuthBean bean = new BankAuthBean();
        bean.mobile = mobile;
        bean.bankcard = bankcard;
        bean.verify_code = verify_code;
        Api.api_service.getBankAuth(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "认证成功");
                    finish();
                });
    }

    public void getSmsSend(String mobile) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        SmsSendBean bean = new SmsSendBean();
        bean.mobile = mobile;
        bean.type = "sms";
        Api.api_service.getSmsSend(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "发送成功");
                    timer.start();
                });
    }

    private void init() {
        txtTitle.setText("银行卡认证");

        UI.setTvHtml(txtBankTips, R.string.bank_bottom_tips);
        Util.setBankChange(editBankNo);
        editIdno.setText(UserInfoGlobal.getIdNo());

        editName.setText(UserInfoGlobal.getRealName());
        selectAreaDilaog = new SelectAreaDilaog(this).setOnChooseListener(new SelectAreaDilaog.OnChooseListener() {
            @Override
            public void onAddressSelect(String provinceId, String provinceName, String cityId, String cityName, String areaId, String areaName) {
                if (TextUtils.isEmpty(provinceName)) {
                    provinceName = "";
                }
                if (TextUtils.isEmpty(provinceName)) {
                    cityName = "";
                }
                if (TextUtils.isEmpty(provinceName)) {
                    areaName = "";
                }
                editAddress.setText(provinceName + cityName + areaName);
            }
        });

        selectMenuTipsDilaog = new SelectMenuTipsDilaog(this).setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
            @Override
            public void onLeft() {
                finish();
            }

            @Override
            public void onRight() {
                selectMenuTipsDilaog.dismiss();
            }
        }).showTwoSelectData("主人", R.mipmap.img_idcard_fail_dialog, "只有认证成功才能借款啊，您确定要离开吗？", "残忍离开", "我要留下");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        unbiner.unbind();
        unbiner = null;
    }

    @Override
    public void onBackPressed() {
        selectMenuTipsDilaog.show();
    }


    @OnClick({R.id.edit_address, R.id.btn_back, R.id.btn_send_code, R.id.txt_bank_tips, R.id.cb_check, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_send_code:
                if (UI.toString(editPhone).length() != 11) {
                    ToastUtil.makeText(this, "请输入正确的手机号");
                    return;
                }
                getSmsSend(UI.toString(editPhone));
                break;
            case R.id.txt_bank_tips:
                Util.startActivity(this, BankSupportActivity.class);
                break;
            case R.id.cb_check:
                break;
            case R.id.btn_submit:
                if (UI.toString(editPhone).length() != 11) {
                    ToastUtil.makeText(this, "请输入正确的手机号");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editBankNo))) {
                    ToastUtil.makeText(this, "请输入正确的银行卡号");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editCode))) {
                    ToastUtil.makeText(this, "请输入正确的验证码");
                    return;
                }
                getBankAuth(UI.toString(editPhone), UI.toString(editBankNo).replace(" ", ""), UI.toString(editCode));
                break;
            case R.id.edit_address:
                break;
        }
    }
}
